package com.dd.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dd.entity.DdUser;
import com.dd.entity.DdUserpay;
import com.dd.util.DateStrFormat;
import com.dd.util.PageList;

public class UserpayAction extends BaseAction {
	public ActionForward queryUserPay(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");

		PrintWriter out = response.getWriter();
		Integer logid = (Integer) request.getSession().getAttribute(
				"systemlogid");// 系统用户是否已经登录

		if (logid == null) {// 商家用户尚未登录
			// 返回-1
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>-1</result></root>");
			return null;
		}
		StringBuffer sbf = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
		String name = request.getParameter("username");
		String status = request.getParameter("status");
		String type = request.getParameter("type");
		String stratcreatedate = request.getParameter("stratcreatedate");// 申请退款开始时间
		String endcreatedate = request.getParameter("endcreatedate");// 申请退款结束时间
		String stratpaydate = request.getParameter("stratpaydate");// 确认取消退款开始时间
		String endpaydate = request.getParameter("endpaydate");// 确认/取消退款结束时间
		String pageNo = request.getParameter("pageno");// 第几页
		String pageSize = request.getParameter("pagesize");// 每页记录数
		int i_pageNo = 1;
		if (pageNo != null && pageNo.trim().length() > 0) {
			i_pageNo = Integer.parseInt(pageNo);
		} else {
			i_pageNo = 1;
		}
		int i_pageSize = 10;
		if (pageSize != null && pageSize.trim().length() > 0) {
			i_pageSize = Integer.parseInt(pageSize);
		} else {
			i_pageSize = 10;
		}
		StringBuffer sqlwhere = new StringBuffer(" ");
		if (name != null && name.trim().length() > 0) {
			sqlwhere.append(" and b.name like '%" + name + "%'");
		}
		if (type != null && type.trim().length() > 0) {
			sqlwhere.append(" and a.paytype in (" + type + ")");
		}
		if (status != null && status.trim().length() > 0) {
			sqlwhere.append(" and a.status in (" + status + ")");
		}
		if (stratcreatedate != null && stratcreatedate.trim().length() > 0) {
			sqlwhere.append(" and a.createdate >= '" + stratcreatedate + "'");
		}
		if (endcreatedate != null && endcreatedate.trim().length() > 0) {
			sqlwhere.append(" and a.createdate <= '" + endcreatedate + "'");
		}
		if (stratpaydate != null && stratpaydate.trim().length() > 0) {
			sqlwhere.append(" and a.paydate >= '" + stratpaydate + "'");
		}
		if (endpaydate != null && endpaydate.trim().length() > 0) {
			sqlwhere.append(" and a.paydate <= '" + endpaydate + "'");
		}
		PageList pl = this.getDdUserpayDAO().findByTypeAndStatusAndWhere(
				sqlwhere.toString(), i_pageNo, i_pageSize);
		sbf.append("<pays total=\"" + pl.getTotalCount() + "\">");
		for (int i = 0; i < pl.size(); i++) {
			Object[] obj = (Object[]) pl.get(i);
			DdUserpay dup = (DdUserpay) obj[0];
			DdUser du = (DdUser) obj[1];
			sbf.append("<pay userid=\"" + du.getId() + "\" username=\""
					+ du.getName() + "\" id=\"" + dup.getId() + "\" payid=\""
					+ (dup.getId() + 200000000) + "\" paytype=\""
					+ dup.getPaytype() + "\" paydate=\""
					+ DateStrFormat.getCurrentTime(dup.getPaydate())
					+ "\" amount=\"" + dup.getAmount() + "\" paystatus=\""
					+ dup.getStatus() + "\" createdate=\""
					+ DateStrFormat.getCurrentTime(dup.getCreatedate())
					+ "\" status=\"" + dup.getStatus() + "\" status_str=\""
					+ getStatus(dup.getStatus()) + "\"/>");
		}
		sbf.append("</pays></root>");
		out.print(sbf.toString());
		return null;
	}

	public ActionForward updateUserPay(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");

		PrintWriter out = response.getWriter();
		Integer logid = (Integer) request.getSession().getAttribute(
				"systemlogid");// 系统用户是否已经登录

		if (logid == null) {// 商家用户尚未登录
			// 返回-1
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>-1</result></root>");
			return null;
		}
		String id = request.getParameter("id");
		String status = request.getParameter("status");
		DdUserpay dup = this.getDdUserpayDAO().findById(Integer.parseInt(id));
		if (dup == null) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>2</result></root>");
			return null;
		}
		dup.setStatus(Integer.parseInt(status));
		dup.setPaydate(new Date());
		try {
			this.getDdUserpayDAO().saveOrUpdate(dup);
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>1</result></root>");
		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>0</result></root>");
		}
		return null;
	}

	private String getStatus(int status) {
		if (status == 1) {
			return "申请退款";
		}
		if (status == 2) {
			return "确认退款";
		}
		if (status == 3) {
			return "取消退款";
		}
		return "";
	}

	public ActionForward paylist(ActionMapping mapping, ActionForm pcUserForm,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		HttpSession session = request.getSession();
		StringBuffer sbf = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
		PrintWriter out = response.getWriter();
		try {
			Integer logid = (Integer) request.getSession().getAttribute(
					"systemlogid");// 系统用户是否已经登录
			if (logid == null) {// 商家用户尚未登录
				// 返回-1
				out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>-1</result></root>");
				return null;
			}
			String userid = request.getParameter("userid");
			if (userid == null && userid.trim().length() == 0) {//用户ID为空
				out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>-2</result></root>");
				return null;
			}
			List list = this.getDdUserpayDAO().findByUserid(
					Integer.parseInt(userid));
			for (int i = 0; i < list.size(); i++) {
				DdUserpay dup = (DdUserpay) list.get(i);
				if (dup.getStatus().intValue() == 1
						&& dup.getPaytype().intValue() == 1) {
					continue;
				}
				float ft = dup.getAmount() + dup.getAmountfree();
				int scale = 2;// 设置位数
				int roundingMode = 4;// 表示四舍五入，可以选择其他舍值方式，例如去尾，等等.
				BigDecimal bd = new BigDecimal((double) ft);
				bd = bd.setScale(scale, roundingMode);
				ft = bd.floatValue();
				sbf.append("<pay id=\"" + dup.getId() + "\" payid=\""
						+ (dup.getId() + 200000000) + "\" paytype=\""
						+ dup.getPaytype() + "\" paydate=\""
						+ DateStrFormat.getCurrentTime(dup.getPaydate())
						+ "\" amount=\"" + ft + "\" paystatus=\""
						+ dup.getStatus() + "\" remark=\"" + dup.getRemark()
						+ "\" />");
			}
			sbf.append("</root>");
			out.print(sbf.toString());
			return null;
		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}
}
